package com.ruoyi.business.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.AppUser;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.service.IAppUserService;
import com.ruoyi.system.service.ISysConfigService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 客户信息
 *
 * @author 石涛
 */
@RestController
@RequestMapping("/system/appUser")
public class SysAppUserController extends BaseController {
    @Resource
    private IAppUserService appUserService;

    @Autowired
    private ISysConfigService configService;

    /**
     * 获取所有客户列表
     */
    @ApiOperation("获取所有客户")
    @PreAuthorize("@ss.hasPermi('system:appUser:allUser')")
    @GetMapping("/allUser")
    public AjaxResult allUser() {
        List<AppUser> appUsers = appUserService.selectAllAppUserList();
        return success(appUsers);
    }

    @ApiOperation("获取客户通过手机号")
    @PreAuthorize("@ss.hasPermi('system:appUser:getListByMobile')")
    @GetMapping("/getListByMobile")
    public AjaxResult getListByMobile(AppUser appUser) {
        List<AppUser> appUsers = appUserService.selectAllAppUserListByMobile(appUser);
        return success(appUsers);
    }

    /**
     * 下载模版
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<AppUser> util = new ExcelUtil<AppUser>(AppUser.class);
        util.importTemplateExcel(response, "客户数据", "客户数据模版");
    }

    @Log(title = "导入客户数据", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('system:appUser:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<AppUser> util = new ExcelUtil<AppUser>(AppUser.class);
        List<AppUser> userList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = appUserService.importAppUser(userList, updateSupport, operName);
        Map<String, String> map = new HashMap<>();
        map.put("result", message);
        return success(map);
    }

    /**
     * 获取客户列表
     */
    @PreAuthorize("@ss.hasPermi('system:appUser:list')")
    @GetMapping("/list")
    public TableDataInfo list(AppUser appUser) {
        startPage();
        List<AppUser> list = appUserService.getAppUserList(appUser);
        return getDataTable(list);
    }

    /**
     * 新增客户数据
     */
    @PreAuthorize("@ss.hasPermi('system:appUser:insert')")
    @PostMapping("/insert")
    public AjaxResult insert(@RequestBody AppUser appUser) {
        System.out.println("客户信息：" + appUser.toString());
        List<AppUser> appUsers = appUserService.selectAllAppUserListByMobile(appUser);
        if (!appUsers.isEmpty()) {
//            String result
            return error(String.format("手机号为【%s】的用户已经存在，请勿重复添加！！！", appUser.getMobile()));
        }
        String password = configService.selectConfigByKey("sys.user.initPassword");
        appUser.setPassword(SecurityUtils.encryptPassword(password));
        appUserService.insertAppUser(appUser);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("data", "新增客户成功");
        return ajax;
    }

    /**
     * 更新客户数据
     */
    @PreAuthorize("@ss.hasPermi('system:appUser:update')")
    @PostMapping("/update")
    public AjaxResult update(@RequestBody AppUser appUser) {
        System.out.println("客户信息：" + appUser.toString());
        appUserService.updateAppUser(appUser);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("data", "修改客户成功");
        return ajax;
    }

    /**
     * 更新客户是否是管理员
     */
    @PreAuthorize("@ss.hasPermi('system:appUser:auth')")
    @PostMapping("/auth")
    public AjaxResult setAuth(@RequestBody AppUser appUser) {
        AppUser appUser1 = appUserService.selectAppUserById(appUser.getUserId());
        appUser1.setIsAdmin(appUser.getIsAdmin());
        appUser1.setUpdateBy(getUsername());
        appUserService.updateAppUser(appUser1);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("data", "修改客户成功");
        return ajax;
    }

    @PreAuthorize("@ss.hasPermi('system:appUser:remove')")
    @Log(title = "删除客户", businessType = BusinessType.DELETE)
    @DeleteMapping("/{userIds}")
    public AjaxResult remove(@PathVariable Long[] userIds) {
        appUserService.deleteAppUserByIds(userIds);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("data", "删除客户成功");
        return ajax;
    }

}
